package com.kgd.news.utils;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.handler.Handler;
import com.kgd.news.entity.SysUser;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017年3月11日 下午6:10:25
 * @Time: 拦截页面请求
 */
public class PermissionHandler extends Handler {

	@Override
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		try{
			if (target.indexOf("/admin/") != -1 && (target.endsWith(".html") || target.endsWith(".jsp"))) {
				SysUser user = (SysUser) request.getSession().getAttribute(Constants.request_cUser);
				System.out.println("user is :::::" + user);
				if (user == null) {
					try {
//						没有登陆的
						response.sendRedirect("/index.html");
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			next.handle(target, request, response, isHandled);
		}catch (Exception e){
			e.printStackTrace();
			try {
				response.sendRedirect("/index.html");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

}
